package com.shopnight.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.*;

@EnableAsync
@Configuration
public class MyThreadConfig {
    @Autowired
    ThreadPoolConfigProperties threadPoolConfigProperties;

    @Bean(name = "taskExecutor")
    public ThreadPoolTaskExecutor asyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(threadPoolConfigProperties.getCoreSize());
        executor.setMaxPoolSize(threadPoolConfigProperties.getMaxSize());
        executor.setKeepAliveSeconds(threadPoolConfigProperties.getKeepAliveTime());
        executor.setQueueCapacity(threadPoolConfigProperties.getQueueCapacity());
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }


}
